package com.example.mall.web.controller;

import com.alibaba.fastjson.JSON;
import com.example.mall.domain.Products;
import com.example.mall.dto.ProductsDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductsQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IProductsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductsController {

    @Autowired
    public IProductsService productsService;

    /**
     * 商品上架
     */
//    @PreAuthorize("hasAuthority('paroduct:online')")
    @RequestMapping(value="/onLineProduct/{id}",method= RequestMethod.POST)
    @ApiOperation(value = "商品上架",notes = "商品上架",httpMethod = "POST")
    public JSONResult onLineProduct(@PathVariable("id")Long id){
        productsService.onLineProduct(id);
        return JSONResult.success();
    }

    /**
     * 商品下架
     */
//    @PreAuthorize("hasAuthority('paroduct:offline')")
    @RequestMapping(value="/offLineProduct/{id}",method= RequestMethod.POST)
    @ApiOperation(value = "商品下架",notes = "商品下架",httpMethod = "POST")
    public JSONResult offLineProduct(@PathVariable("id")Long id){
        productsService.offLineProduct(id);
        return JSONResult.success();
    }
    /**
     * 获取商品详情
     */
    @RequestMapping(value = "/info/json/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "获取商品详情",notes = "获取商品详情",httpMethod = "GET")
    public JSONResult getCourseInfoForJSON(@PathVariable("id")Long id){
        return JSONResult.success(JSON.toJSONString(productsService.selectProductInfoById(id)));
    }

    /**
     * 获取商品详情ids
     */
    @RequestMapping(value = "/infos/json",method = RequestMethod.POST)
    @ApiOperation(value = "获取商品详情ids",notes = "获取商品详情ids",httpMethod = "GET")
    public JSONResult getCourseInfosForJSON(@RequestBody List<Long> courseIds){
        return JSONResult.success(JSON.toJSONString(productsService.selectProductInfosById(courseIds)));
    }

    /**
     * 分页查询商品基本信息表信息
     * @param productsQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询商品基本信息表信息",notes = "分页查询商品基本信息表信息",httpMethod = "GET")
    public JSONResult queryPage(ProductsQuery productsQuery){
        PageDTO<ProductsDto> loginPageDTO = productsService.queryPage(productsQuery);
        return JSONResult.success("查询成功",loginPageDTO);
    }

    /**
     * 根据id查询商品基本信息表信息
     * @param id
     * @return
     */
    @GetMapping("query/{id}")
    @ApiOperation(value = "根据id查询商品基本信息表信息",notes = "根据id查询商品基本信息表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("id")Long id){
        //1.根据id查询用户信息
        if (id == null){
            throw new GlobalCustomerException(ErrorEnumCode.PRODUCTID_NOT_ID);
        }
        Products products = productsService.getById(id);
        return JSONResult.success("查询成功",products);
    }

    /**
     * 根据id查询商品基本信息表信息
     * @param id
     * @return
     */
    @GetMapping("queryProductsById/{id}")
    @ApiOperation(value = "根据id查询商品基本信息表信息",notes = "根据id查询商品基本信息表信息",httpMethod = "GET")
    public JSONResult queryProductsById(@PathVariable("id")Long id){
        //1.根据id查询用户信息
        if (id == null){
            throw new GlobalCustomerException(ErrorEnumCode.PRODUCTID_NOT_ID);
        }
        Products products = productsService.queryProductsById(id);
        return JSONResult.success("查询成功",products);
    }

    /**
     * 查询全部商品基本信息表信息
     * @param
     * @return
     */
    @GetMapping("queryProductsList")
    @ApiOperation(value = "查询全部商品基本信息表信息",notes = "查询全部商品基本信息表信息",httpMethod = "GET")
    public JSONResult queryProductsList(){
        List<Products> products = productsService.queryProductsList();
        return JSONResult.success("查询成功",products);
    }

    /**
     * 新增商品基本信息表
     * @param productsDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增商品基本信息表",notes = "新增商品基本信息表",httpMethod = "POST")
    public JSONResult insert(@RequestBody ProductsDto productsDto){
        productsService.insert(productsDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改商品基本信息表信息
     * @param productsDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改商品基本信息表信息",notes = "修改商品基本信息表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody ProductsDto productsDto){
        productsService.update(productsDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除商品基本信息表信息
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除商品基本信息表信息",notes = "删除商品基本信息表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("id") Long id) {
        productsService.delete(id);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除商品基本信息表信息
     * @param ids
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除商品基本信息表信息",notes = "批量删除商品基本信息表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> ids) {
        productsService.deleteBatch(ids);
        return JSONResult.success("批量删除成功");
    }
}
